package com.krtech.wecard.module.mayi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.krtech.wecard.module.mayi.entity.MayiPayList;
import com.krtech.wecard.module.mayi.entity.ServcieMachineCount;
import com.krtech.wecard.module.mayi.entity.StatisticsQueryVo;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;

@Mapper
public interface MayiPayListMapper extends BaseMapper<MayiPayList> {
    /**
     * 量自增
     *
     * @param orderNumber
     * @return
     */
    @Update("update mayi_pay_list set reach_count = (reach_count + 1) where order_no = #{orderNumber}")
    int incre(String orderNumber);

    /**
     * 获得当前完成任务中overTime最大的任务
     *
     * @return
     */
    @Select("select * from mayi_pay_list where `status` = 3 ORDER BY end_time DESC limit 1")
    MayiPayList getMaxOverTime();

    /**
     * 获得正在执行和等待的数量
     *
     * @return
     */
    @Select("select * from mayi_pay_list where `status` in (1,2)")
    List<MayiPayList> getWaitCountList();

    /**
     * 对某一行上锁，避免并发问题
     *
     * @param id
     */
    boolean lock(Integer id);

    /**
     * 返还卡的次数
     */
    @Update("update mayi_cdk_manage set current_use = (current_use - #{returnCount}) where cdk_code = #{cdkCode}")
    boolean returnCdk(@Param("cdkCode") String cdkCode, @Param("returnCount") Integer returnCount);

    /**
     * 统计查询
     */
    Integer queryStatistics(StatisticsQueryVo queryVo);

    Integer statisticsDianzan(StatisticsQueryVo queryVo);

    /**
     * 获得cdk剩余量
     *
     * @param cdkCode
     * @return
     */
    @Select("select sum(goal_count) from mayi_pay_list where cdk_code = #{cdkCode} and (reach_count > real_goal_count)")
    Long getCdkUse(String cdkCode);

    /**
     * 设置 dianzan在payList表的已完成数量为dianzanLog的count数量
     */
    @Update("update mayi_pay_list set reach_count = #{dianzanCount} where order_no = #{orderNumber}")
    int updateDianzanCount(@Param("orderNumber") String orderNumber,@Param("dianzanCount") Integer dianzanCount);

    /**
     * 获得各个任务服务 机器的数量
     */
    @Select("select fk_order_no,count(1) as machine_count from mayi_machine_manage where `status` = 1 GROUP BY fk_order_no")
    List<ServcieMachineCount> getServiceMachineCount();

    /**
     * 查找 进行中 却 未完成的点赞任务，修复其redis
     */
    @Select("select * from mayi_pay_list where type = 2  and `status` in (1,2)")
    public List<MayiPayList> getOnlineDianzan();

    @Select("select * from mayi_pay_list where type = 3 and reach_count < goal_count and `status` in (1,2)")
    public List<MayiPayList> getOnlineComment();
}